/*
 * AacRtpUnPacker.cpp
 *
 *  Created on: 2018年4月21日
 *      Author: zhengboyuan
 */

#include "AacRtpUnPacker.h"

#define AU_HEADER_SIZE  4

namespace av
{

AacRtpUnPacker::AacRtpUnPacker():
		m_mediaType(MEDIA_TYPE_AUDIO),
		m_format()
{
}

AacRtpUnPacker::~AacRtpUnPacker()
{
}

bool AacRtpUnPacker::open(int mediaType, int codec)
{
	m_format.m_audioCodec = MEDIA_CODEC_AAC;

	return true;
}

bool AacRtpUnPacker::unpack(RtpPacket& pktIn, MediaPacket& pktOut)
{
	bool found = false;
	if (pktIn.size < AU_HEADER_SIZE)
	{
		return false;
	}

	ByteBuffer& buffer = m_buffer.getCurBuffer();
	buffer.write(pktIn.data + AU_HEADER_SIZE, pktIn.size - AU_HEADER_SIZE);

	if (pktIn.mark)
	{
		pktOut.set_type(m_mediaType);
		pktOut.set_dts(pktIn.ts);
		pktOut.set_duration(0);
		pktOut.copy(buffer.data(), buffer.size());

		m_buffer.switchBuffer();
		m_buffer.getCurBuffer().clear();

		found = true;
	}
	return found;
}

void AacRtpUnPacker::close()
{
	m_buffer.clear();
}

bool AacRtpUnPacker::getFormat(MediaFormat& format)
{
	format = m_format;
	return true;
}



} /* namespace av */
